package com.rainbow.support.minio.config;
import com.rainbow.support.minio.properties.MinioProperties;
import com.rainbow.support.minio.toolkit.MinioUtil;
import io.minio.MinioClient;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * minio配置项
 * @author 付为地
 */
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
@ConditionalOnProperty(prefix=MinioProperties.MINIO_PROPERTIES_PRFIX,name = "enable", havingValue = "true",matchIfMissing = true)
public class MinioConfig {

    @Autowired
    private MinioProperties minioProperties;

    @SneakyThrows
    @Bean
    public MinioClient getMinioClient() {
        return new MinioClient(minioProperties.getUrl(), minioProperties.getAccess(), minioProperties.getSecret());
    }

    @Bean
    @ConditionalOnBean(MinioClient.class)
    public MinioUtil minioUtil() {
        return new MinioUtil(getMinioClient());
    }

}
